import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'react-redux';

import m1 from './middlewares/m1';
import m2 from './middlewares/m2';

const defaultState = {
    list: ['task1', 'task2', 'task3']
};

const reducer = (state = defaultState, action) => {
    switch (action.type) {
    case 'LOAD_DATA':
        return state;
    case 'PUT_DATA':
        return {
            list: [...state.list, action.task]
        };
    case 'DELETE_DATA':
        return {
            list: state.list.filter((i, idx) => idx !== action.task)
        };
    case 'LOAD_LIST':
        return {
            list: [...action.task]
        };
    default:
        return state;
    }
};

// 中间件一旦挂上, 就会被中间件拦下来
const midddleware = applyMiddleware(thunk); // redux 可以进行异步操作

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducer, /* preloadedState, */ composeEnhancers(
    midddleware
));

export default store;
